/**
 * Copyright (c) 2024,  科泰集团教研团队
 * 版权所有，请勿侵权
 */
package com.ktjiaoyu.news.dao.impl;

import com.ktjiaoyu.news.dao.BaseDao;
import com.ktjiaoyu.news.dao.NewsCollectDao;
import com.ktjiaoyu.news.entity.NewsCollect;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * 会员可以收藏新闻方便后续查看 
 * @author 科泰集团教研团队
 * @createTime 2024-4-17 20:49:54
 * @lastModify 2024-4-17 20:49:54
 * @version 1.0.0
 */
public class NewsCollectDaoImpl extends BaseDao implements NewsCollectDao {

    @Override
    public List<NewsCollect> getPage(NewsCollect entity, Integer pageNo, Integer pageSize) {
        List<NewsCollect> list = new ArrayList<>();
        Connection conn = null;
        PreparedStatement stmt = null;
        ResultSet rs = null;
        try {
            conn = getConnection();
            List<Object> params = new ArrayList<>();
            String sql = "select * from news_collect where 1=1 ";
            sql += createPageCondition(entity, params);
            // sql += " order by xxx , xxx desc"

            sql += " limit ?, ?";
            params.add((pageNo - 1) * pageSize);
            params.add(pageSize);
            stmt = conn.prepareStatement(sql);
            for (int i = 0; i < params.size(); i++) {
                stmt.setObject(i + 1, params.get(i));
            }
            rs = stmt.executeQuery();
            while(rs.next()){
                NewsCollect newsCollect = new NewsCollect();
                newsCollect.setId(rs.getLong("id"));
                newsCollect.setNewsId(rs.getLong("newsId"));
                newsCollect.setMemberId(rs.getLong("memberId"));
                newsCollect.setCollectTime(rs.getDate("collectTime"));
                list.add(newsCollect);
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            closeAll(conn, stmt, rs);
        }
        return list;
    }

    @Override
    public int count(NewsCollect entity) {
        Connection conn = null;
        PreparedStatement stmt = null;
        ResultSet rs = null;
        try {
            conn = getConnection();
            List<Object> params = new ArrayList<>();
            String sql = "select count(1) from news_collect where 1=1 ";
            sql += createPageCondition(entity, params);
            stmt = conn.prepareStatement(sql);
            for (int i = 0; i < params.size(); i++) {
                stmt.setObject(i + 1, params.get(i));
            }
            rs = stmt.executeQuery();
            while(rs.next()){
                return rs.getInt(1);
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            closeAll(conn, stmt, rs);
        }
        return 0;
    }

    @Override
    public List<NewsCollect> getAll() {
        List<NewsCollect> list = new ArrayList<>();
        Connection conn = null;
        PreparedStatement stmt = null;
        ResultSet rs = null;
        try {
            conn = getConnection();
            List<Object> params = new ArrayList<>();
            String sql = "select * from news_collect";
            stmt = conn.prepareStatement(sql);
            rs = stmt.executeQuery();
            while(rs.next()){
                NewsCollect newsCollect = new NewsCollect();
                newsCollect.setId(rs.getLong("id"));
                newsCollect.setNewsId(rs.getLong("newsId"));
                newsCollect.setMemberId(rs.getLong("memberId"));
                newsCollect.setCollectTime(rs.getDate("collectTime"));
                list.add(newsCollect);
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            closeAll(conn, stmt, rs);
        }
        return list;
    }

    @Override
    public NewsCollect getById(Long id) {
        NewsCollect newsCollect = null;
        Connection conn = null;
        PreparedStatement stmt = null;
        ResultSet rs = null;
        try {
            // 获取连接
            conn = getConnection();
            List<Object> params = new ArrayList<>();
            String sql = "select * from news_collect where id=?";
            // 获取执行sql对象，设置参数
            stmt = conn.prepareStatement(sql);
            stmt.setObject(1, id);
            // 执行获得结果
            rs = stmt.executeQuery();
            while(rs.next()){
                newsCollect = new NewsCollect();
                newsCollect.setId(rs.getLong("id"));
                newsCollect.setNewsId(rs.getLong("newsId"));
                newsCollect.setMemberId(rs.getLong("memberId"));
                newsCollect.setCollectTime(rs.getDate("collectTime"));
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            closeAll(conn, stmt, rs);
        }
        return newsCollect;
    }

    @Override
    public int insert(NewsCollect entity) {
        return executeUpdate("INSERT INTO news_collect(`newsId`,`memberId`,`collectTime`) VALUES (?,?,?);"
        ,entity.getNewsId(),entity.getMemberId(),entity.getCollectTime());
    }

    @Override
    public int[] insertBatch(NewsCollect... entities) {
        int[] rows = null;
        Connection conn = null;
        PreparedStatement stmt = null;
        ResultSet rs = null;
        try {
            conn = getConnection();
            conn.setAutoCommit(false);
            String sql = "INSERT INTO newsCollect(`newsId`,`memberId`,`collectTime`) VALUES (?,?,?);";
            stmt = conn.prepareStatement(sql);
            for (int i = 0; i < entities.length; i++) {
                NewsCollect entity = entities[i];
                if(entity == null){
                    continue;
                }
                int j = 1;
                stmt.setObject(j++, entity.getNewsId());
                stmt.setObject(j++, entity.getMemberId());
                stmt.setObject(j++, entity.getCollectTime());
                stmt.addBatch();
            }
            rows = stmt.executeBatch();
            stmt.clearBatch();
            conn.commit();
        }catch (Exception e){
            e.printStackTrace();
            if(conn != null){
                try {
                    conn.rollback();
                } catch (SQLException ex) {
                    ex.printStackTrace();
                }
            }
        }finally {
            closeAll(conn, stmt, rs);
        }
        return rows;
    }

    @Override
    public int update(NewsCollect entity) {
       if(entity == null || entity.getId() == null){
           return 0;
       }
       List<Object> params = new ArrayList<>();
       String sql = "UPDATE `news_collect` SET ";
                if(entity.getNewsId() != null){
                    sql += " newsId = ? ,";
                    params.add(entity.getNewsId());
                }
                if(entity.getMemberId() != null){
                    sql += " memberId = ? ,";
                    params.add(entity.getMemberId());
                }
                if(entity.getCollectTime() != null){
                    sql += " collectTime = ? ,";
                    params.add(entity.getCollectTime());
                }
       if(params.size() > 0){
           // 去除逗号
           sql = sql.substring(0, sql.length() - 1);
           sql += " WHERE id = ? ";
           params.add(entity.getId());
           return executeUpdate(sql, params.toArray());
       }
       return 0;
    }

    @Override
    public int deleteByEntity(NewsCollect entity)  {
        if(entity == null){
            return 0;
        }
        List<Object> params = new ArrayList<>();
        String sql = "delete from news_collect where 1=1 ";
        if(entity.getId() != null){
            sql += " and id = ?";
            params.add(entity.getId());
        }
        if(entity.getNewsId() != null){
            sql += " and newsId = ?";
            params.add(entity.getNewsId());
        }
        if(entity.getMemberId() != null){
            sql += " and memberId = ?";
            params.add(entity.getMemberId());
        }
        if(entity.getCollectTime() != null){
            sql += " and collectTime = ?";
            params.add(entity.getCollectTime());
        }
        if(params.size() == 0){
            return 0;
        }
        return executeUpdate(sql, params.toArray());
    }

    @Override
    public int deleteById(Long id) {
        return executeUpdate("delete from news_collect where id=?", id);
    }

    @Override
    public int[] deleteBatch(Long... ids) {
        int[] rows = null;
        Connection conn = null;
        PreparedStatement stmt = null;
        ResultSet rs = null;
        try {
            conn = getConnection();
            conn.setAutoCommit(false);
            String sql = "delete from news_collect where id=?";
            stmt = conn.prepareStatement(sql);
            for (int i = 0; i < ids.length; i++) {
                Long id = ids[i];
                if(id == null){
                    continue;
                }
                int j = 1;
                stmt.setObject(j++, id);
                stmt.addBatch();
            }
            rows = stmt.executeBatch();
            stmt.clearBatch();
            conn.commit();
        }catch (Exception e){
            e.printStackTrace();
            if(conn != null){
                try {
                    conn.rollback();
                } catch (SQLException ex) {
                    ex.printStackTrace();
                }
            }
        }finally {
            closeAll(conn, stmt, rs);
        }
        return rows;
    }

    private String createPageCondition(NewsCollect entity, List<Object> params) {
        String sql = "";
        if(entity != null){
        if(entity.getId() != null){
            sql += " and id = ?";
            params.add(entity.getId());
        }
        if(entity.getNewsId() != null){
            sql += " and newsId = ?";
            params.add(entity.getNewsId());
        }
        if(entity.getMemberId() != null){
            sql += " and memberId = ?";
            params.add(entity.getMemberId());
        }
        if(entity.getCollectTime() != null){
            sql += " and collectTime = ?";
            params.add(entity.getCollectTime());
        }
        }
        return sql;
    }
}
